/*
 * @Author: Aaron.wei 
 * @Date: 2018-11-28 10:53:28 
 * @Last Modified by: Aaron.wei
 * @Last Modified time: 2018-12-29 18:14:23
 * @Version: 1.0
 */
 var global_search_time;
 var util = {
  common: {
    init: function () {
      this.events();
    },
    events: function () {
      // main header Change Lang
      $(document).on('click', '.j-change-lang .dropdown-item', function() {
        var current = $('#userLang').data('current'),
            _icon = $(this).data('icon'),
            _txt = $(this).text(),
            _lang = $(this).data('lang');
        if (current !== _lang) {
          // 切换语言, 可在此做异步请求
          $('#userLang').data('current', _lang);
          $('#userLang img').attr('src', _icon);
          $('#userLang span').text(_txt)
        }
      });
    },
    search: function () {
      //search drive
      $(document).off('change', '.j-tpv-search input[type="text"]').on('keyup', '.j-tpv-search input[type="text"]', function() {
        if (typeof search === 'function') {
        	if (window.global_search_time) {
                clearTimeout(window.global_search_time);
            }
            window.global_search_time = setTimeout(function() {
            	search();
            },300);//300毫秒
        }
      });

      $(document).off('change', '.j-tpv-search select').on('change', '.j-tpv-search select', function() {
        if (typeof search === 'function') {
          search();
        }
      })

      $(document).off('change', '.j-tpv-search input[type="checkbox"]').on('change', '.j-tpv-search input[type="checkbox"]', function() {
        if (typeof search === 'function') {
          search();
        }
      })

      //add by aaron.wei 2018.12.29
      $(document).on('keydown', '.j-tpv-search', function(event) {
        if(event.keyCode==13){
          if (typeof search === 'function') {
            search();
          }
          return false;
        }
      })
    },
    /**
   * data格式化
   * @param date
   * @param fmt
   * @returns {*}
   */
    dateFormatting: function (date, fmt) {
      if (!fmt) {
        fmt = 'yyyy-MM-dd hh:mm:ss'
      }
      var weekday = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
      var o = {
        'M+': date.getMonth() + 1, // 月份
        'd+': date.getDate(), // 日
        'h+': date.getHours(), // 小时
        'm+': date.getMinutes(), // 分
        's+': date.getSeconds(), // 秒
        'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
        'W+': weekday[date.getDay()], // 星期
        'S': date.getMilliseconds() // 毫秒
      }
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      for (var k in o) {
        if (new RegExp('(' + k + ')').test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
        }
      }
      return fmt
    },
    /**
     * 时间戳转时间字符串
     * @param date
     * @param fmt
     * @returns {*}
     */
    getDateStr: function (date, fmt) {
      if (!fmt) {
        fmt = 'yyyy-MM-dd hh:mm:ss'
      }
      let newDate = new Date()
      newDate.setTime(date * 1000)
      return this.dateFormatting(newDate, fmt)
    },
    /**
     * 时间字符串转时间戳
     * @param Str
     * @returns {*}
     */
    getDateTime: function (Str) {
      var timestamp = new Date().getTime();
      if (Str) {
        Str = Str.substring(0,19);
    	  Str = Str.replace(/-/g,'/'); 
    	  timestamp = new Date(Str).getTime();
      }
    	return timestamp
    }
  }
 }